.PHONY: help build run stop restart logs clean test health download-model

# 默认目标
help:
	@echo "YOLO11 - NPU推理部署"
	@echo ""
	@echo "可用命令:"
	@echo "  make build          - 构建Docker镜像（NPU版本）"
	@echo "  make run            - 启动服务"
	@echo "  make stop           - 停止服务"
	@echo "  make restart        - 重启服务"
	@echo "  make logs           - 查看日志"
	@echo "  make health         - 健康检查"
	@echo "  make test           - 运行测试"
	@echo "  make clean          - 清理容器和镜像"
	@echo "  make download-model - 下载YOLO11模型"

# 构建Docker镜像（NPU版本）
build:
	@echo "构建Docker镜像（NPU版本）..."
	docker build -t yolo11-npu:latest .

# 使用docker-compose启动服务
run:
	@echo "启动YOLO11 API服务..."
	docker-compose up -d

# 停止服务
stop:
	@echo "停止服务..."
	docker-compose down

# 重启服务
restart:
	@echo "重启服务..."
	docker-compose restart

# 查看日志
logs:
	docker-compose logs -f

# 健康检查
health:
	@echo "执行健康检查..."
	@curl -s http://localhost:18004/health | python3 -m json.tool || echo "服务未运行或不可访问"

# 运行测试
test:
	@echo "运行API测试..."
	@chmod +x test_api.sh
	./test_api.sh

# 清理
clean:
	@echo "清理容器和镜像..."
	docker-compose down -v
	docker rmi yolo11-npu:latest 2>/dev/null || true

# 下载YOLO11模型
download-model:
	@echo "下载YOLO11模型..."
	@mkdir -p /Users/yanlp/models
	@python3 -c "from ultralytics import YOLO; model = YOLO('yolo11n.pt'); print('模型下载完成')"
	@echo "模型已保存到 ~/.cache/ultralytics/"
	@echo "请将模型复制到 /Users/yanlp/models/yolo11n.pt"

# 开发模式（本地运行）
dev:
	@echo "启动开发模式..."
	python3 -m uvicorn api.main:app --reload --host 0.0.0.0 --port 8000
